package com.taxigang.client.publish;

import com.google.gwt.user.client.Command;
import com.taxigang.client.Session;
import com.taxigang.client.facebook.FacebookApi;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.uibinder.InvitePopup;
import com.taxigang.shared.TaxiGangInfo;

public class Publisher {

	public void publish(){
		final TaxiGangInfo info = Session.getInstance().getCurrentMyTaxiGang();
		String infoText = invitationMessage(info);
		Session.getInstance().setRef(infoText);

		if (info != null){
			Command cmd = new Command(){

				@Override
				public void execute() {
					//String link = "http://127.0.0.1:8888/TaxiGangGWT.html?gwt.codesvr=127.0.0.1:9997/?tg="+info.getId();
					//					String locale = Session.getInstance().getLocale();
					//					String link = "http://apps.facebook.com/taxigang/?tg="+info.getId()+"&locale="+locale;
					String link = info.getLink();
					String invitationText = Session.getInstance().getRef();
					FacebookApi.postTaxigang("/me/feed",invitationText+" "+link,link,FacebookApi.getDumper());

					if (!Session.getInstance().getLocalHost()){
						FacebookApi.postTaxigang("/176271679134329/feed",invitationText+" "+link,link,FacebookApi.getDumper());

					} else {
						String startLat = Double.toString(info.getStartLat());
						String startLng = Double.toString(info.getStartLng());
						String destLat = Double.toString(info.getDestinationLat());
						String destLng = Double.toString(info.getDestinationLng());

						String path = "search?q=*&type=place&center=LAT,LNG&distance=100";
						path = path.replace("LAT", startLat);
						path = path.replace("LNG", startLng);
						
//						FacebookApi.search(path,invitationText,link);

					}
				}

			};
			InvitePopup.invite(cmd, TaxiGangCreator.taxiGangConstants.wantToPostMe());
		}

	}

	public static String invitationMessage(TaxiGangInfo info){
		String msgBody = TaxiGangCreator.taxiGangConstants.invitationGreeting()+"\n";;
		msgBody += TaxiGangCreator.taxiGangConstants.from()+":"+info.getStart()+"\n";
		msgBody += TaxiGangCreator.taxiGangConstants.to()+":"+info.getDestination()+"\n";
		msgBody += TaxiGangCreator.taxiGangConstants.time()+":"+info.getDepartureTime()+"\n";
		msgBody += TaxiGangCreator.taxiGangConstants.invitationLink()+"\n\n";
		return msgBody;
	}

}
